from sklearn.cross_decomposition import PLSRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler #归一化
from sklearn.metrics import confusion_matrix,recall_score,classification_report,accuracy_score
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import GridSearchCV
import warnings
from sklearn.model_selection import validation_curve
from sklearn.model_selection import KFold
import numpy as np


#读取特征矩阵
spec = pd.read_excel('correct_spec.xlsx')
x = np.array(spec)
#我这里的特征x形状： (939, 150)


#做一个标签向量，标签y形状： (939,)
y1 = np.zeros((189,1))
y2 = 1*np.ones((188,1))
y3 = 2*np.ones((185,1))
y4 = 3*np.ones((188,1))
y5 = 4*np.ones((189,1))
y = np.vstack((y1,y2,y3,y4,y5))
y = y.ravel()

#先做一个数据集的划分
train_X,test_X, train_y, test_y = train_test_split(x,  y, test_size=0.2)

#然后对y进行转换
train_y = pd.get_dummies(train_y)

#建模
model = PLSRegression(n_components=8)
model.fit(train_X,train_y)

#预测
y_pred = model.predict(test_X)

#将预测结果（类别矩阵）转换为数值标签
y_pred = np.array([np.argmax(i) for i in y_pred])

#模型评价
print('测试集混淆矩阵为：\n',confusion_matrix(test_y,y_pred))
print('平均分类准确率为：\n',accuracy_score(test_y,y_pred))